<?php
$path =  str_replace('/wp-content/plugins/stSocialMediaToolbox/Toolboxes/stPublishToolbox/cronjob','',dirname(__FILE__));
require($path.'/wp-config.php');
$config = include $path.'/wp-content/plugins/stSocialMediaToolbox/Toolboxes/stPublishToolbox/config.php';
require_once($path.'/wp-content/plugins/stSocialMediaToolbox/Toolboxes/stTwitterToolbox/libs/tmhOAuth.php');
global $wpdb;
$data['cronjob_name']  = 'cronjob Twitter Publish';
$data['toolbox_name'] = 'stPublishToolbox';
$data['url'] = dirname(__FILE__).'/wall_publish.php';
$data['status'] = 0;
$data['ctime'] = date_i18n('Y-m-d H:i:S');
$wpdb->insert('wp_st_cronjob_log',$data);
$log_id = $wpdb->get_var("SELECT MAX(id) FROM wp_st_cronjob_log");
foreach($wpdb->get_results("SELECT * FROM st_publishing WHERE published=1 AND publish_type='twitter'") as $row){
    $date_array = explode(' ',$row->time);
    $date = explode('-',$date_array[0]);
    $time = explode(':',$date_array[1]);
    $current =  mktime ($time[0],$time[1],00 ,intval($date[1]) ,intval($date[0]) ,$date[2] );
    if($current <= mktime(date_i18n("H"),date_i18n("i"),date_i18n("s"),date_i18n("n"),date_i18n("j"),date_i18n("Y"))){
       
                         post_tweet($row->content."\n".$row->media,$row->user_id);
                      $wpdb->update('st_publishing',array('published'=>0),array('id'=>$row->id));
    }
}
$wpdb->update('wp_st_cronjob_log',array('status'=>1),array('id'=>$log_id));
function post_tweet($tweet_text,$id) {
        $options = cronjob_get_option('stTwitterToolbox_1_settings', $id);
        $tmhOAuth = new tmhOAuth(array(
        'consumer_key'    => $config['twitter_consumer_key'],
        'consumer_secret' => $config['twitter_consumer_secret'],
        'user_token' => $options['user_token'],
        'user_secret' => $options['user_secrect'],
      ));

      $code = $tmhOAuth->request('POST', $tmhOAuth->url('1/statuses/update'), array(
            'status' => $tweet_text
        ));
    }
    function cronjob_get_option($option, $user_id = 0, $default = false) {
            global $wpdb;

            if (false) {
                $optionTable = $wpdb->options;
                $where = 'option_name = %s LIMIT 1';
            } else {
                $optionTable = 'wp_st_options';
                if($user_id == 0)
                    $user_id = get_current_user_id();
                $where = "user_id = $user_id AND option_name = %s LIMIT 1";


            $option = trim($option);
            if (empty($option))
                return false;

            if (defined('WP_SETUP_CONFIG'))
                return false;
            $value = wp_cache_get($option, 'options');
            if (false === $value) {
                $suppress = $wpdb->suppress_errors();
                $row = $wpdb->get_row($wpdb->prepare("SELECT option_value FROM $optionTable WHERE {$where} ", $option));
                $wpdb->suppress_errors($suppress);
                if (is_object($row)){
                    $value = $row->option_value;
                    wp_cache_add($option, $value, 'options');
                }else
                    return $default;
            }

            return apply_filters('option_' . $option, maybe_unserialize($value));
        }
    }

?>
